package com.zat.mes.andrmes.customerview

import android.content.Context
import android.util.AttributeSet
import android.view.View
import android.widget.RelativeLayout
import androidx.core.content.ContextCompat
import com.bigkoo.pickerview.builder.TimePickerBuilder
import com.bigkoo.pickerview.listener.OnTimeSelectListener
import com.bigkoo.pickerview.view.TimePickerView
import com.contrarywind.view.WheelView
import com.zat.mes.andrmes.R
import com.zat.mes.andrmes.utils.DateUtil
import kotlinx.android.synthetic.main.layout_head_clock.view.*
import java.util.*

/**
 * @author zy
 * @date :2020-02-25 13:19
 * @description: 日历选择投
 */
class HeadDateView : RelativeLayout {


    private var timePicker: TimePickerView? = null
    var showDate: String? = null

    constructor(context: Context) : super(context) {

    }

    constructor(context: Context, attributeSet: AttributeSet) : super(context, attributeSet) {

    }

    constructor(context: Context, attributeSet: AttributeSet, defStyleAttr: Int) : super(
        context,
        attributeSet,
        defStyleAttr
    ) {

    }

    init {


    }

    /**
     * 结束时间
     */
    override fun onFinishInflate() {
        super.onFinishInflate()
        showDate =
            DateUtil.formatDate(Date(), context.getString(R.string.head_date)) + DateUtil.getWeek(
                Date()
            )
        setDateText()
        initDatePick()
        llCalenClock.setOnClickListener { timePicker?.show() }
    }

    /**
     * 初始化
     */
    private fun initDatePick() {
        timePicker = TimePickerBuilder(context, object : OnTimeSelectListener {
            override fun onTimeSelect(date: Date?, v: View?) {
                if (date != null) {
                    headTimeSelectListener?.onTimeSelect(
                        DateUtil.formatDate(
                            date,
                            DateUtil.FORMAT_3
                        )
                    )
                    showDate = DateUtil.formatDate(
                        date,
                        context.getString(R.string.head_date)
                    ) + DateUtil.getWeek(date)
                    setDateText()


                }
            }

        }).setSubmitColor(ContextCompat.getColor(context, R.color.color_text_nomal))
            .setCancelColor(ContextCompat.getColor(context, R.color.color_text_nomal))
            .setTitleBgColor(ContextCompat.getColor(context, R.color.color_white))
            .setContentTextSize(18)
            .setLabel("", "", "", "", "", "")
            .setDividerType(WheelView.DividerType.WRAP)
            .setDividerColor(ContextCompat.getColor(context, R.color.color_theme))
            .setLineSpacingMultiplier(2.0f)
            .build()
    }

    /**
     * 设置日期
     */
    fun setDateText() {
        tvCanlendarClock.text = showDate
    }

    /**
     * 设置用户名
     */
    fun setUserName(name: String) {
        tvNameClock.text = name
    }

    /**
     * 时间时间
     */
    fun setTimeName(name: String) {
        tvNameHeadClock.text = name;
    }

    var headTimeSelectListener: OnHeadTimeSelectListener? = null

    /**
     * 时间选择监听
     */
    interface OnHeadTimeSelectListener {
        fun onTimeSelect(date: String)
    }
}